package sy.controller;

import java.io.File;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.jackson.AppEntryLogMixin;
import sy.jackson.SysProjectMixin;
import sy.model.IotEntranceURI;
import sy.model.IotPatrolURI;
import sy.model.IotTalkBackURI;
import sy.model.SysAccount;
import sy.model.SysProject;
import sy.service.AppBusinessServiceI;
import sy.service.IotEntranceURIServiceI;
import sy.service.IotPatrolURIServiceI;
import sy.service.IotTalkBackURIServiceI;
import sy.service.SysAccountAppFunctionMappingServiceI;
import sy.service.SysOwnerRenterMappingServiceI;
import sy.service.SysUserServiceI;
import sy.util.HttpUtils;
import sy.util.ResultJson;

@Controller
@RequestMapping("v1/appBusiness")
public class AppBusinessController {
	@Autowired
	SysUserServiceI sysUserService;
	@Autowired
	SysOwnerRenterMappingServiceI sysOwnerRenterMappingService;
	@Autowired
	SysAccountAppFunctionMappingServiceI sysAccountAppFunctionMappingService;
	@Autowired
	private AppBusinessServiceI appBusinessService;
	@Autowired
	IotEntranceURIServiceI iotEntranceURIService;
	@Autowired
	IotTalkBackURIServiceI iotTalkBackURIService;
	@Autowired
	IotPatrolURIServiceI iotPatrolURIService;

	private static final ObjectMapper objectMapper = new ObjectMapper();
	/**
	 * 业主同住人---获取
	 * @Title: getVerificatCode
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月16日 上午11:04:35
	 * @throws
	 */
	@RequestMapping(value="/ownerRenter/getList", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getOwnerRenterList(@RequestParam("ownerId") String ownerId, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.listSysUserByOwnerId(ownerId,token);
		return resultJson;
	}
	/**
	 * 业主同住人---添加（授权）
	 * @Title: addRenterVisitor
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月16日 下午1:44:07
	 * @throws
	 */
	@RequestMapping(value="/ownerRenter/add", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson addOwnerRenter(@RequestParam("ownerId") String ownerId, @RequestParam("telephone") String telephone,
			@RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.addOwnerRenter(ownerId, telephone, token);
		return resultJson;
	}
	/**
	 * 业主同住人---删除
	 * @Title: deleteRenterVisitor
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月16日 下午3:26:29
	 * @throws
	 */
	@RequestMapping(value="/ownerRenter/delete", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson deleteOwnerRenter(@RequestParam("ownerId") String ownerId,@RequestParam("renterId") String renterId, 
			@RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.deleteOwnerRenter(ownerId, renterId,token);
		return resultJson;
	}
	/**
	 * 功能列表---调整
	 * @Title: adjustFunction
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月17日 上午11:21:40
	 * @throws
	 */
	@RequestMapping(value="/funciton/adjust", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson adjustFunction(@RequestParam("ownerId") String ownerId, @RequestParam("functionIds") String functionIdsStr, 
			@RequestParam("token") String token){
		List<Integer> functionIds = new ArrayList<Integer>();
		String[] strArray = functionIdsStr.split(",");
		for(String str : strArray){
			functionIds.add(Integer.parseInt(str));
		}
		ResultJson resultJson = appBusinessService.adjustFunction(ownerId, functionIds, token);
		return resultJson;
	}
	/**
	 * 扫一扫---获取信息
	 * @Title: getScanData
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月17日 下午1:34:01
	 * @throws
	 */
	@RequestMapping(value="/qrcode/scan", method=RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String getScanData(@RequestParam("type") String type, @RequestParam("code") String code, 
			@RequestParam("token") String token){
		objectMapper.addMixInAnnotations(SysProject.class, SysProjectMixin.BasicInfo.class);
		ResultJson resultJson = appBusinessService.getScanData(type, code, token);
		try {
			return objectMapper.writeValueAsString(resultJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 设备报修---新增
	 * @Title: repairAdd
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月18日 上午8:45:09
	 * @throws
	 */
	@RequestMapping(value="/repair/add", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson addRepair(HttpServletRequest request, HttpServletResponse response, @RequestParam("ownerId") String ownerId,
			@RequestParam("equipmentId") String equipmentId, @RequestParam("type") String type, @RequestParam("phenomenon") String phenomenon,
			@RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.addRepair(request, response, ownerId, equipmentId, type, phenomenon, token);
		return resultJson;
	}
	/**
	 * 设备报修---获取(主要是获取到报修单号供评价时调用)
	 * @Title: getRepairList
	 * @Description: TODO
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月18日 上午9:09:29
	 * @throws
	 */
	@RequestMapping(value="/repair/getList", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getRepairList(@RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.getRepairList(token);
		return resultJson;
	}
	/**
	 * 多文件上传
	 * @Title: fileUpload
	 * @Description: TODO
	 * @param: @param request
	 * @param: @param response
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月18日 下午3:33:25
	 * @throws
	 */
	@RequestMapping(value="/fileUpload", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson fileUpload(HttpServletRequest request,HttpServletResponse response, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.fileUpload(request, response);
		return resultJson;
	}
	/**
	 * 评价
	 * @Title: comment
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月18日 下午4:52:42
	 * @throws
	 */
	@RequestMapping(value="/comment", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson comment(@RequestParam("ownerId") String ownerId, @RequestParam("type") String type,
			@RequestParam("repairCode") String repairCode, @RequestParam("content") String content, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.comment(ownerId, type, content, repairCode,token);
		return resultJson;
	}
	/**
	 * 
	 * @Title		:modifyPersonInfo
	 * @Description	:TODO
	 * @param		:@param sex
	 * @param		:@param name
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:Administrator
	 * @time		:2017年9月19日 上午9:46:14
	 * @throws
	 */
	@RequestMapping(value="/user/modify", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson modifyPersonInfo(@RequestParam(value="sex",required=false) String sex,@RequestParam(value="name",required=false) String name,@RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.modifyPersonInfo(sex,name,token);
		return resultJson;
	}
	/**
	 * 
	 * @Title		:updateComlaint
	 * @Description	:TODO
	 * @param		:@param status
	 * @param		:@param code
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:Administrator
	 * @time		:2017年9月19日 上午9:30:55
	 * @throws
	 */
	@RequestMapping(value="/complaint/update", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson updateComlaint(@RequestParam("status") String status, @RequestParam("code") String code, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.updateComplaintStatus(code, status, token);
		return resultJson;
	}
	
	/**
	 * 获取投诉内容
	 * @Title: complaintGetList
	 * @Description: TODO
	 * @param: @param status
	 * @param: @param code
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月23日 上午10:41:28
	 * @throws
	 */
	@RequestMapping(value="/complaint/getList", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson listComlaint(@RequestParam("ownerId") String ownerId, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.listComplaint(ownerId,token);
		return resultJson;
	}
	/**
	 * 获取个人动态
	 * @Title: listFriends
	 * @Description: TODO
	 * @param: @param accountId
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月26日 下午2:06:29
	 * @throws
	 */
	@RequestMapping(value="/friends/getPersonList", method=RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String listPersonFriends(@RequestParam("accountId") String accountId, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.listAppFriendsByAccountId(accountId,token);
		objectMapper.addMixInAnnotations(SysProject.class, SysProjectMixin.BasicInfo.class);
		try {
			return objectMapper.writeValueAsString(resultJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";	
	}
	/**
	 * 获取社区动态
	 * @Title: listFriends
	 * @Description: TODO
	 * @param: @param projectId
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月28日 上午9:17:24
	 * @throws
	 */
	@RequestMapping(value="/friends/getList", method=RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String listFriends(@RequestParam("projectId") String projectId, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.listAppFriendsProjectId(projectId,token);
		objectMapper.addMixInAnnotations(SysProject.class, SysProjectMixin.BasicInfo.class);
		try {
			return objectMapper.writeValueAsString(resultJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";	
	}
	/**
	 * 发表个人动态
	 * @Title: addFriends
	 * @Description: TODO
	 * @param: @param request
	 * @param: @param response
	 * @param: @param accountId
	 * @param: @param projectId
	 * @param: @param content
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月26日 下午2:50:13
	 * @throws
	 */
	@RequestMapping(value="/friends/add", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson addFriends(HttpServletRequest request,HttpServletResponse response, @RequestParam("accountId") String accountId, 
			@RequestParam("projectId") String projectId, @RequestParam("content") String content, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.addAppFriends(request, response, accountId, projectId, content, token);
		return resultJson;
	}
	/**
	 * 删除朋友圈动态
	 * @Title: deleteFriends
	 * @Description: TODO
	 * @param: @param friendsId
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月26日 下午2:51:56
	 * @throws
	 */
	@RequestMapping(value="/friends/delete", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson deleteFriends(@RequestParam("friendsId") String friendsId, @RequestParam("token") String token){
		ResultJson resultJson = appBusinessService.deleteAppFriendsById(friendsId,token);
		return resultJson;
	}
	/***************************************************物联模块**************************************************************/
	
	/***************门禁************/
	/**
	 * 
	 * @Title		:门禁鉴权（test）
	 * @Description	:TODO
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月8日 下午1:57:24
	 * @throws
	 */
	@RequestMapping(value="/iot/entrance/getDoorId",method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getDoorId(){
		HttpUtils http  = new HttpUtils();
		ResultJson resultJson = new ResultJson();
		String message = null;
		String uniqueCode = "getDoorIdGET";
		IotEntranceURI iotEntranceURI = iotEntranceURIService.getIotEntranceURIByCode(uniqueCode);
		
		try {
			String result  = http.sendGetRest(iotEntranceURI.getUri(), "GET");
			JSONObject objectResult1 = JSONObject.parseObject(result);
			String JQResult =(String)objectResult1.get("JQResult");
			JSONObject objectResult2 =JSONObject.parseObject(JQResult);
			
			message = (String)objectResult2.get("XX");
			JSONObject jsonObj  = objectResult2.getJSONArray("MLB").getJSONObject(0);
			
			resultJson.setState(true);
			resultJson.setMessage("门ID:"+jsonObj.get("MID")+"鉴权"+message); 
		    resultJson.setErrorCode(0);
		    
		    /* int iSize = jsonArray.size();
		    for (int i = 0; i < iSize; i++) {
		    	JSONObject jsonObj = jsonArray.getJSONObject(i);
		    	resultJson.setMessage("门ID:"+jsonObj.get("MID")+"开门"+message); 
		    }*/
			
		} catch (Exception e) {
			resultJson.setState(false);
			resultJson.setMessage("鉴权失败");
			resultJson.setErrorCode(1);
			e.printStackTrace();
		}
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:开门（test）
	 * @Description	:TODO
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月8日 下午1:57:24
	 * @throws
	 */
	@RequestMapping(value="/iot/entrance/openDoor",method=RequestMethod.GET)
	@ResponseBody
	public ResultJson openDoor(){
		HttpUtils http  = new HttpUtils();
		ResultJson resultJson = new ResultJson();
		String message = null;
		String uniqueCode = "openDoorGET";
		IotEntranceURI iotEntranceURI = iotEntranceURIService.getIotEntranceURIByCode(uniqueCode);
		
		try {
			String result  = http.sendGetRest(iotEntranceURI.getUri(), "GET");
			JSONObject objectResult1 = JSONObject.parseObject(result);
			String JQResult =(String)objectResult1.get("KMResult");
			JSONObject objectResult2 =JSONObject.parseObject(JQResult);
			
			message = (String)objectResult2.get("XX");
			resultJson.setState(true);
			resultJson.setMessage(message); 
		    resultJson.setErrorCode(0);
			
		} catch (Exception e) {
			resultJson.setState(false);
			resultJson.setMessage("开门失败");
			resultJson.setErrorCode(1);
			e.printStackTrace();
		}
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:门禁开门
	 * @Description	:TODO
	 * @param		:@param reqId
	 * @param		:@param upid
	 * @param		:@param bldgId
	 * @param		:@param unitId
	 * @param		:@param personType
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月8日 下午2:43:27
	 * @throws
	 */
	@RequestMapping(value="/iot/entrance/openDoor",method=RequestMethod.POST)
	@ResponseBody
	public ResultJson openDoor(@RequestParam("reqId") String reqId, @RequestParam("upid") String upid, @RequestParam("bldgId") String bldgId,
			@RequestParam("unitId") String unitId,@RequestParam("personType") String personType){
		HttpUtils http  = new HttpUtils();
		ResultJson resultJson = new ResultJson();
		String message = null;
		String uniqueCode = "authenticatePOST";
		String uniqueCode2 = "openDoorPOST";
		IotEntranceURI iotEntranceURI = iotEntranceURIService.getIotEntranceURIByCode(uniqueCode);
		IotEntranceURI iotEntranceURI2 = iotEntranceURIService.getIotEntranceURIByCode(uniqueCode2);
		
		try {
			Map<String, String> parametersAuthenticate = new HashMap<String, String>();  
	        parametersAuthenticate.put("reqId", reqId);  
	        parametersAuthenticate.put("upid", upid);
	        parametersAuthenticate.put("bldgId", bldgId);
	        parametersAuthenticate.put("unitId", unitId);  
	        parametersAuthenticate.put("personType", personType);
			String result  = http.sendPost(iotEntranceURI.getUri(), parametersAuthenticate);
			JSONObject objectResult1 = JSONObject.parseObject(result);
			String JQResult =(String)objectResult1.get("JQResult");
			JSONObject objectResult2 =JSONObject.parseObject(JQResult);
			
			message = (String)objectResult2.get("XX");
			JSONObject jsonObj  = objectResult2.getJSONArray("MLB").getJSONObject(0);
			String doorId = (String)jsonObj.get("MID");
			

			Map<String, String> parametersOpendoor = new HashMap<String, String>();  
			parametersOpendoor.put("reqId", reqId);  
			parametersOpendoor.put("upid", upid);
			parametersOpendoor.put("doorId", doorId);
			String resultData  = http.sendPost(iotEntranceURI2.getUri(), parametersOpendoor);
			JSONObject objectResultData = JSONObject.parseObject(resultData);
			String JQResultData =(String)objectResultData.get("KMResult");
			JSONObject objectResultdata2 =JSONObject.parseObject(JQResultData);
				
			message = (String)objectResultdata2.get("XX");
			float[] dataArray;//定义
			dataArray = new float[0];//空数组赋值
	
			resultJson.setState(true);
			resultJson.setMessage("开门成功"); 
			resultJson.setResult(dataArray);
			resultJson.setErrorCode(200);
			
		} catch (Exception e) {
			resultJson.setState(false);
			resultJson.setMessage("系统异常，操作失败");
			resultJson.setErrorCode(201);
			e.printStackTrace();
		}
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:开门
	 * @Description	:TODO
	 * @param		:@param reqId
	 * @param		:@param upid
	 * @param		:@param doorId
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月8日 下午3:19:11
	 * @throws
	 */
/*	@RequestMapping(value="/iot/entrance/openDoor",method=RequestMethod.POST)
	@ResponseBody
	public ResultJson openDoor(@RequestParam("reqId") String reqId, @RequestParam("upid") String upid, @RequestParam("doorId") String doorId){
		HttpUtils http  = new HttpUtils();
		ResultJson resultJson = new ResultJson();
		String message = null;
		String uniqueCode = "openDoorPOST";
		IotEntranceURI iotEntranceURI = iotEntranceURIService.getIotEntranceURIByCode(uniqueCode);
		
		try {
			Map<String, String> parametersAuthenticate = new HashMap<String, String>();  
	        parametersAuthenticate.put("reqId", reqId);  
	        parametersAuthenticate.put("upid", upid);
	        parametersAuthenticate.put("doorId", doorId);
			String result  = http.sendPost(iotEntranceURI.getUri(), parametersAuthenticate);
			JSONObject objectResult1 = JSONObject.parseObject(result);
			String JQResult =(String)objectResult1.get("KMResult");
			JSONObject objectResult2 =JSONObject.parseObject(JQResult);
			
			message = (String)objectResult2.get("XX");
			float[] dataArray;//定义
			dataArray = new float[0];//空数组赋值

			resultJson.setState(true);
			resultJson.setMessage("开门成功"); 
			resultJson.setResult(dataArray);
		    resultJson.setErrorCode(200);
			
		} catch (Exception e) {
			resultJson.setState(false);
			resultJson.setMessage("系统异常，操作失败");
			resultJson.setErrorCode(201);
			e.printStackTrace();
		}
		return resultJson;
	}*/
	
	/**
	 * 
	 * @Title		:saveEntryLog
	 * @Description	:TODO
	 * @param		:@param blueToothOrginName
	 * @param		:@param blueToothCustomName
	 * @param		:@param openTime
	 * @param		:@param accountId
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:xieyuanqiu
	 * @time		:2017年9月11日 下午3:43:19
	 * @throws
	 */
	@RequestMapping(value="/iot/entryLog",method=RequestMethod.POST)
	@ResponseBody
	public ResultJson saveEntryLog(@RequestParam(value="blueToothOrginName",required=false) String blueToothOrginName, @RequestParam(value="blueToothCustomName",required=false) String blueToothCustomName, 
			 @RequestParam(value="yzId",required=false) String yzId,@RequestParam(value="wyId",required=false) String wyId,
			 @RequestParam(value="reqId",required=false) String reqId,@RequestParam(value="upid",required=false) String upid,
			@RequestParam("openTime") String openTime, @RequestParam("doorId") String doorId, @RequestParam("type") String type){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("blueToothOrginName", blueToothOrginName);
		map.put("blueToothCustomName", blueToothCustomName);
		map.put("yzId", yzId);
		map.put("wyId", wyId);
		map.put("reqId", reqId);
		map.put("upid", upid);
		map.put("openTime", openTime);
		map.put("doorId", doorId);
		map.put("type", type);
		ResultJson resultJson = appBusinessService.saveEntryLog(map);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:getEntryLog
	 * @Description	:TODO
	 * @param		:@param token
	 * @param		:@return
	 * @return		:String
	 * @author		:xieyuanqiu
	 * @time		:2017年9月11日 下午3:43:36
	 * @throws
	 */
	@RequestMapping(value="/iot/entryLog/{token}",method=RequestMethod.GET,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String getEntryLog(@PathVariable("token") String token){
		ResultJson resultJson = appBusinessService.listAppEntryLog(token);
		objectMapper.addMixInAnnotations(SysAccount.class, AppEntryLogMixin.BasicInfo.class);
		try {
			return objectMapper.writeValueAsString(resultJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/***************机房远程监控************/
	
	/**
	 * 
	 * @Title		:machineProject
	 * @Description	:TODO
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月8日 下午5:14:06
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/project",method=RequestMethod.GET)
	@ResponseBody
	public ResultJson machineProject(){
		HttpUtils http  = new HttpUtils();
		ResultJson resultJson = new ResultJson();
		String message = null;
		String uniqueCode = "project";
		IotTalkBackURI iotTalkBackURI = iotTalkBackURIService.getIotTalkBackURIByCode(uniqueCode);
		
		try {
			String result  = http.sendGetRest(iotTalkBackURI.getUri(), "GET");
			JSONObject jsonObject = JSONObject.parseObject(result);
			JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("data"));
			message = (String)jsonObject.get("errorMessage");

			resultJson.setState(true);
			resultJson.setResult(jsonArray);
			resultJson.setMessage(message); 
		    resultJson.setErrorCode(200);
			
		} catch (Exception e) {
			resultJson.setState(false);
			resultJson.setMessage("初始化失败");
			e.printStackTrace();
		}
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:machineDevice
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:Administrator
	 * @time		:2017年9月8日 下午5:47:11
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/device",method=RequestMethod.POST)
	@ResponseBody
	public ResultJson machineDevice(@RequestParam("ProjectCode") String ProjectCode){
		HttpUtils http  = new HttpUtils();
		ResultJson resultJson = new ResultJson();
		String message = null;
		String uniqueCode = "device";
		IotTalkBackURI iotTalkBackURI = iotTalkBackURIService.getIotTalkBackURIByCode(uniqueCode);
		
		try {
			Map<String, String> parameters = new HashMap<String, String>();  
			parameters.put("ProjectCode", ProjectCode);  
			String result  = http.sendPost(iotTalkBackURI.getUri(), parameters);
			JSONObject jsonObject = JSONObject.parseObject(result);
			
			JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("data"));
			message = (String)jsonObject.get("errorMessage");

			resultJson.setState(true);
			resultJson.setResult(jsonArray);
			resultJson.setMessage(message); 
		    resultJson.setErrorCode(200);
			
		} catch (Exception e) {
			resultJson.setState(false);
			resultJson.setMessage("连接失败");
			resultJson.setErrorCode(1);
			e.printStackTrace();
		}
		return resultJson;
	}
	
	/***************巡更平台************/
	
	/**
	 * 
	 * @Title		:machineProject
	 * @Description	:TODO
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月8日 下午5:14:06
	 * @throws
	 */
	@RequestMapping(value="/iot/patrol/request_token",method=RequestMethod.GET)
	@ResponseBody
	public ResultJson patrolRequestToken(){
		HttpUtils http  = new HttpUtils();
		ResultJson resultJson = new ResultJson();
		String message = null;
		String uniqueCode = "requestToken";
		IotPatrolURI iotPatrolURI = iotPatrolURIService.getIotPatrolURIByCode(uniqueCode);
		
		try {
			String result  = http.sendGetRest(iotPatrolURI.getUri(), "GET");
			JSONObject jsonObject = JSONObject.parseObject(result);

			resultJson.setState(true);
			resultJson.setMessage("获取成功"); 
			resultJson.setResult(jsonObject);
		    resultJson.setErrorCode(200);
			
		} catch (Exception e) {
			resultJson.setState(false);
			resultJson.setMessage("获取失败");
			e.printStackTrace();
		}
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:patrolAreas
	 * @Description	:TODO
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:Administrator
	 * @time		:2017年9月9日 下午1:46:18
	 * @throws
	 */
	@RequestMapping(value="/iot/patrol/areas",method=RequestMethod.GET)
	@ResponseBody
	public ResultJson patrolAreas(){ 
		HttpUtils http  = new HttpUtils();
		ResultJson resultJson = new ResultJson();
		String message = null;
		String uniqueCode = "areas";
		IotPatrolURI iotPatrolURI = iotPatrolURIService.getIotPatrolURIByCode(uniqueCode);
		
		try {
			String result  = http.sendGetRest(iotPatrolURI.getUri(), "GET");
			JSONArray jsonOArray = JSONArray.parseArray(result);

			resultJson.setState(true);
			resultJson.setMessage("获取成功"); 
			resultJson.setResult(jsonOArray);
		    resultJson.setErrorCode(200);
			
		} catch (Exception e) {
			resultJson.setState(false);
			resultJson.setMessage("获取失败");
			e.printStackTrace();
		}
		return resultJson;
	}
	
	
	/**
	 * 业主邀请访客
	 * @Title: inviteVistor
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月25日 下午5:44:48
	 * @throws
	 */
	@RequestMapping(value="/visitor/invitation", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson inviteVistor(@RequestParam("token") String token,@RequestParam("name") String name,@RequestParam("telephone") String telephone,
			@RequestParam("accessTime") String accessTime,@RequestParam("doorId") String doorId,@RequestParam(value="carNo",required=false) String carNo){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		params.put("telephone", telephone);
		params.put("accessTime", accessTime);
		params.put("doorId", doorId);
		params.put("carNo", carNo);
		ResultJson resultJson = appBusinessService.inviteVistor(token, params);
		return resultJson;
	}
	
	
	
}

